﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kugar.Core.ExtMethod;

namespace Kugar.Tools.SQLHelper.SQLGenerator.ViewT4
{
    public partial class ControllerGenerator
    {
        public string ModuleName { set; get; }

        public string Namespace { set; get; }

        public DbTableStructInfo Table { set; get; }

        public string ConvertToGetFieldValueCode(DbTableInfo_Column column)
        {
            var valueType = T4SQLHelper.ConvertColumnTypeToNetType_Type(column.ValueType, column.IsNullable);
            var defaultValue = "";
            if (valueType == typeof(string))
            {
                defaultValue = "\"\"";
            }
            else if (valueType.IsClass)
            {
                defaultValue = "null";
            }
            else if(valueType == typeof(DateTime))
            {
                defaultValue = "DateTime.MinValue";
            }
            else
            {
                defaultValue = valueType.GetDefaultValue().ToStringEx();
            }

            var str =
                $"Request.GetField<{T4SQLHelper.ConvertColumnTypeToNetType(column.ValueType, column.IsNullable)}>(\"{column.Name}\",{defaultValue})";

            return str;
        }

        public string ConvertFieldRequestValidator(DbTableInfo_Column column)
        {
            var type = T4SQLHelper.ConvertColumnTypeToNetType_Type(column.ValueType, column.IsNullable);
            var typeName = T4SQLHelper.ConvertColumnTypeToNetType(column.ValueType, column.IsNullable);
            var str = "";

            if (type==typeof(string))
            {
                if (column.Size>0)
                {
                    str += $".MaxLength({column.Size},{(typeName=="varchar" || typeName=="char"? "true" : "false")})";
                }
            }

            if (column.IsNullable)
            {
                str += ".NotEmpty()";
            }

            return str;
        }
    }
}
